import java.util.Arrays;

public class MyArrayList implements IList{

    public int[] elem;
    public int usedSize;

    public static final int DEFAULT_CAPACITY = 5;


    public boolean isFull(){
        return usedSize == elem.length;

    }

    public void grow(){
        elem = Arrays.copyOf(elem,2*elem.length);

    }
    public void grow(int num){
        elem = Arrays.copyOf(elem,num*elem.length);

    }

    @Override
    public void add(int data) {
        if(isFull()){

            //满了就数组扩容；
            grow();
        }
        elem[usedSize++] = data;
    }

   public void checkPos(int pos){
        if(pos < 0 || pos > usedSize){
            throw new PosBoundsOfException("添加元素时位置不合法");
        }
   }

    @Override
    public void add(int pos, int data) {
        //先检查位置是否合法
        checkPos(pos);
        for (int i = usedSize-1; i >= pos ; i--) {
            elem[i+1] = elem[i];
        }
        elem[pos] = data;

    }

    @Override
    public boolean contains(int toFind) {

        for (int i = 0; i <usedSize ; i++) {
            if (elem[i] == toFind) {
                return true;
            }
            return true;
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {

        for (int i = 0; i < usedSize; i++) {
            if (elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    public boolean isEmpty(){
        return  usedSize == 0;
    }

    @Override
    public int get(int pos) {
        checkPosByGet(pos);
        if (isEmpty()) {
            throw new ListEmptyException("获取位置时顺序表为空");
        }
        return 0;
    }

    public void checkPosByGet(int pos){
        if(pos < 0 || pos >= usedSize){
            throw new PosBoundsOfException("获取元素时pos位置不合法" );
        }
    }


    @Override
    public void set(int pos, int value) {

    }

    @Override
    public void remove(int toRemove) {

    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public void clear() {

    }

    @Override
    public void display() {
        for (int i = 0; i <usedSize ; i++) {
            System.out.println(elem[i]);
        }
    }
}
